package com.bestescortbedsystem.service.impl;

import com.bestescortbedsystem.dao.SesHospitalinfoDao;
import com.bestescortbedsystem.dao.SesHospitalinfoDepartmentDao;
import com.bestescortbedsystem.dao.SesUserDao;
import com.bestescortbedsystem.dao.SesUserIdentityDao;
import com.bestescortbedsystem.entity.*;
import com.bestescortbedsystem.service.DepartmentService;
import com.bestescortbedsystem.util.FindDate;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: DepartmentServiceImpl
 * Package: com.bestescortbedsystem.service.impl
 *
 * @author: guanzepeng
 * @Date: 2021/3/1 20:07 星期一
 * @Description: 医院科室业务层
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {

	@Resource
	private SesHospitalinfoDao sesHospitalinfoDao;

	@Resource
	private SesUserDao sesUserDao;

	@Resource
	private SesUserIdentityDao sesUserIdentityDao;

	@Resource
	private SesHospitalinfoDepartmentDao sesHospitalinfoDepartmentDao;

	/**
	 * 查询医院所对应的科室信息
	 * @param sesHospitalinfoDepartment 传入对象
	 * @param pageNum 分页
	 * @param pageSize 分页
	 * @return 返回Map
	 */
	@Override
	public Map<String, Object> findBydepartment(SesHospitalinfoDepartment sesHospitalinfoDepartment, Integer pageNum, Integer pageSize) {
		SesHospitalinfoDepartmentExample sesHospitalinfoDepartmentExample = new SesHospitalinfoDepartmentExample();
		SesHospitalinfoDepartmentExample.Criteria criteria = sesHospitalinfoDepartmentExample.createCriteria();
		//医院ID
		if(sesHospitalinfoDepartment.getDepartmentHospitalinfoid() != null && sesHospitalinfoDepartment.getDepartmentHospitalinfoid() != 0) {
			criteria.andDepartmentHospitalinfoidEqualTo(sesHospitalinfoDepartment.getDepartmentHospitalinfoid());
		}
		//对接人
		if(sesHospitalinfoDepartment.getUserid() != null && sesHospitalinfoDepartment.getUserid() != 0) {
			criteria.andUseridEqualTo(sesHospitalinfoDepartment.getUserid());
		}
		//股东
		if(sesHospitalinfoDepartment.getUsershareholderid() != null && sesHospitalinfoDepartment.getUsershareholderid() != 0) {
			criteria.andUsershareholderidEqualTo(sesHospitalinfoDepartment.getUsershareholderid());
		}
		Map<String, Object> map = new HashMap<>();
		//查询科室
		PageHelper.startPage(pageNum,pageSize);
		List<SesHospitalinfoDepartment> list = sesHospitalinfoDepartmentDao.selectByExample(sesHospitalinfoDepartmentExample);
		PageInfo<SesHospitalinfoDepartment> pageInfo = new PageInfo<>(list);
		map.put("keshi", pageInfo);

		//查询所有科室对接人
		SesUserExample sesUserExample3 = new SesUserExample();
		SesUserExample.Criteria criteria3 = sesUserExample3.createCriteria();
		criteria3.andUsertypeEqualTo(12);
		List<SesUser> users3 = sesUserDao.selectByExample(sesUserExample3);
		map.put("duijieren", users3);

		//查询所有股东
		SesUserExample sesUserExample4 = new SesUserExample();
		SesUserExample.Criteria criteria4 = sesUserExample4.createCriteria();
		criteria4.andUsertypeEqualTo(4);
		List<SesUser> users4 = sesUserDao.selectByExample(sesUserExample4);
		map.put("gudong", users4);

		//一级分销商
		SesUserExample sesUserExample = new SesUserExample();
		SesUserExample.Criteria criteria1 = sesUserExample.createCriteria();
		criteria1.andUsertypeEqualTo(10);
		List<SesUser> users = sesUserDao.selectByExample(sesUserExample);
		map.put("yiji",users);

		//二级分销商
		SesUserExample sesUserExample2 = new SesUserExample();
		SesUserExample.Criteria criteria2 = sesUserExample2.createCriteria();
		criteria2.andUsertypeEqualTo(11);
		List<SesUser> users2 = sesUserDao.selectByExample(sesUserExample2);
		map.put("erji",users2);

		//维修人员
		SesUserExample sesUserExample6 = new SesUserExample();
		SesUserExample.Criteria criteria6 = sesUserExample6.createCriteria();
		criteria6.andUsertypeEqualTo(2);
		List<SesUser> users6 = sesUserDao.selectByExample(sesUserExample6);
		map.put("weixiu",users6);

		//所有医院
		List<SesHospitalinfo> sesHospitalinfos = sesHospitalinfoDao.selectByExample(null);
		map.put("yiyuan",sesHospitalinfos);
		return map;
	}

	/**
	 * 根据医院科室主键ID删除一条科室信息
	 * @param departmentid 传入科室ID
	 * @return 返回Boolean
	 */
	@Override
	public Boolean deleteDepartment(Long departmentid) {
		Boolean b = sesHospitalinfoDepartmentDao.deleteByPrimaryKey(departmentid) == 1;
		return b;
	}

	/**
	 * 查询对接人的详细信息
	 * @param useridentityId 传入用户身份表ID
	 * @return 返回SesUserIdentity
	 */
	@Override
	public SesUserIdentity findByUserIdentity(Long useridentityId) {
		if(useridentityId != null && useridentityId != 0) {
			SesUserIdentity sesUserIdentity = sesUserIdentityDao.selectByPrimaryKey(useridentityId);
			return sesUserIdentity;
		}
		return new SesUserIdentity();
	}

	/**
	 * 根据传入的数据新增一个科室
	 * @param sesHospitalinfoDepartment 传入对象
	 * @return 返回Boolean
	 */
	@Override
	public Boolean insertDepartment(SesHospitalinfoDepartment sesHospitalinfoDepartment) {
		if(sesHospitalinfoDepartment == null) {
			return false;
		}
		sesHospitalinfoDepartment.setDepartmentcreatetime(FindDate.getDate());

		Boolean b = sesHospitalinfoDepartmentDao.insert(sesHospitalinfoDepartment) == 1;
		return b;
	}


}
